"""
@File    :   out.py
@Time    :   2024/03/24 22:10:25
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   用户相关的pydantic序列化模型
"""

from datetime import datetime

from pydantic import BaseModel, Field, TypeAdapter

from src.business.admin.system.user.enums import UserSexEnum
from src.schemas.out import BaseOutSchemaConfigMixin


class UserCompleteAndHavePasswordHashOutSchema(BaseModel, BaseOutSchemaConfigMixin):
    """用户完整且包含password_hash的序列化模型"""

    id: int = Field(default=..., description="主键")
    tenant_id: int = Field(default=..., description="租户ID")
    username: str = Field(default=..., description="用户名")
    password_hash: str = Field(default=..., description="密码hash")
    name: str = Field(default=..., description="使用人姓名")
    phone: str = Field(default=..., description="手机号码")
    email: str = Field(default=..., description="邮箱")
    sex: UserSexEnum = Field(default=..., description="用户性别")
    avatar: str = Field(default=..., description="头像")
    remark: str = Field(default=..., description="备注")
    status: bool = Field(default=..., description="状态")
    is_admin: bool = Field(default=..., description="是否管理员用户")
    create_datetime: datetime = Field(default=..., description="创建时间")
    creator_username: str = Field(default=..., description="创建者")
    updater_username: str = Field(default=..., description="修改者")
    can_login_ip_network: list[str] = Field(
        default=..., description="允许登录的IP段列表"
    )
    number_of_simultaneous_logins: int = Field(
        default=..., description="用户可同时登录数量"
    )
    password_modification_deadline: int = Field(
        default=..., description="密码修改期限(天)"
    )
    # last_login_ip: str = Field(default=..., description="最后登录IP")
    last_login_datetime: datetime = Field(default=..., description="最后登录时间")
    admin_last_modified_datetime: datetime = Field(
        default=..., description="管理员最后修改用户信息的时间"
    )
    admin_pwd_last_reset_datetime: datetime = Field(
        default=..., description="管理员最后重置密码的时间"
    )
    self_info_last_modified_datetime: datetime = Field(
        default=..., description="用户自身最后修改信息的时间"
    )
    self_psw_last_modified_datetime: datetime = Field(
        default=..., description="用户自身密码最后修改时间"
    )


class UserCompleteOutSchema(BaseModel, BaseOutSchemaConfigMixin):
    """用户完整的序列化模型"""

    id: int = Field(default=..., description="主键")
    tenant_id: int = Field(default=..., description="租户ID")
    username: str = Field(default=..., description="用户名")
    name: str = Field(default=..., description="使用人姓名")
    phone: str = Field(default=..., description="手机号码")
    email: str = Field(default=..., description="邮箱")
    sex: UserSexEnum = Field(default=..., description="用户性别")
    avatar: str = Field(default=..., description="头像")
    remark: str = Field(default=..., description="备注")
    status: bool = Field(default=..., description="状态")
    is_admin: bool = Field(default=..., description="是否管理员用户")
    create_datetime: datetime = Field(default=..., description="创建时间")
    creator_username: str = Field(default=..., description="创建者")
    updater_username: str = Field(default=..., description="修改者")
    can_login_ip_network: list[str] = Field(
        default=..., description="允许登录的IP段列表"
    )
    number_of_simultaneous_logins: int = Field(
        default=..., description="用户可同时登录数量"
    )
    password_modification_deadline: int = Field(
        default=..., description="密码修改期限(天)"
    )
    # last_login_ip: str = Field(default=..., description="最后登录IP")
    last_login_datetime: datetime = Field(default=..., description="最后登录时间")
    admin_last_modified_datetime: datetime = Field(
        default=..., description="管理员最后修改用户信息的时间"
    )
    admin_pwd_last_reset_datetime: datetime = Field(
        default=..., description="管理员最后重置密码的时间"
    )
    self_info_last_modified_datetime: datetime = Field(
        default=..., description="用户自身最后修改信息的时间"
    )
    self_psw_last_modified_datetime: datetime = Field(
        default=..., description="用户自身密码最后修改时间"
    )


# 完整的用户序列化列表模型
ListUserCompleteOutSchema = TypeAdapter(list[UserCompleteOutSchema])


class UserSelfInfoOutSchema(BaseModel, BaseOutSchemaConfigMixin):
    """用户自身信息序列化模型"""

    id: int = Field(default=..., description="主键")
    name: str = Field(default=..., description="使用人姓名")
    avatar: str = Field(default=..., description="头像")
    is_admin: bool = Field(default=..., description="是否管理员用户")


class UserSelfAndMenuOutSchema(BaseModel, BaseOutSchemaConfigMixin):
    """用户自身及其关联菜单的序列化模型"""

    user: UserSelfInfoOutSchema = Field(default=..., description="用户自身信息")
    menus: list[dict] = Field(default_factory=list, description="菜单信息列表")
    menu_permissions: list[str] = Field(
        default_factory=list, description="菜单权限标识列表"
    )
